package fudan.se.lab2.service;

import fudan.se.lab2.controller.request.CommentPostRequest;
import fudan.se.lab2.controller.request.OpenPostRequest;
import fudan.se.lab2.controller.request.RebuttalRequest;
import fudan.se.lab2.domain.*;
import fudan.se.lab2.exception.MeetingWrongStatusException;
import fudan.se.lab2.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PostService {
    private final PostRepository postRepository;
    private final RebuttalRepository rebuttalRepository;
    private final DistributionRepository distributionRepository;
    private final MeetingRepository meetingRepository;
    private final ContributionRepository contributionRepository;

    @Autowired
    public PostService(PostRepository postRepository, RebuttalRepository rebuttalRepository, DistributionRepository distributionRepository, MeetingRepository meetingRepository, ContributionRepository contributionRepository) {
        this.postRepository = postRepository;
        this.rebuttalRepository = rebuttalRepository;
        this.distributionRepository = distributionRepository;
        this.meetingRepository = meetingRepository;
        this.contributionRepository = contributionRepository;
    }




    public Post commentPost(CommentPostRequest request) {
        Post post = postRepository.findByMeetingFullNameAndTitleAndType(request.getMeetingFullName(), request.getTitle(), request.getType());
        if (post.getPcMembers().isEmpty()) {
            List<String> pcMembers = new ArrayList<>();
            pcMembers.add(request.getPcMember());
            List<String> comments = new ArrayList<>();
            comments.add(request.getComment());
            post.setPcMembers(pcMembers);
            post.setComments(comments);
        } else {
            post.getPcMembers().add(request.getPcMember());
            post.getComments().add(request.getComment());
        }
        postRepository.save(post);
        return post;
    }

    public Rebuttal sendRebuttal(RebuttalRequest request) {
        Meeting meeting = meetingRepository.findMeetingByFullName(request.getMeetingFullName());
        if (meeting.getStatus() != 4)
            throw new MeetingWrongStatusException("Chair还未发布第一次结果");
        if (rebuttalRepository.findByContributorAndMeetingFullNameAndTitle(request.getContributor(), request.getMeetingFullName(), request.getTitle()) == null) {
            Rebuttal rebuttal = new Rebuttal(request.getTitle(), request.getMeetingFullName(), request.getContributor(), request.getComments());
            rebuttalRepository.save(rebuttal);
            Post post = new Post(request.getTitle(), request.getMeetingFullName(), 2);
            postRepository.save(post);
            Contribution contribution = contributionRepository.findByContributorAndMeetingFullNameAndFileTitle(request.getContributor(),request.getMeetingFullName(),request.getTitle());
            contribution.setStatus(2);
            contributionRepository.save(contribution);
            return rebuttal;
        } else {
            return null;
        }
    }

    public List<Post> allPostIJoined(String username) {
        List<Post> posts = new ArrayList<>();
        Post post;
        Iterable<Distribution> distributions = distributionRepository.findByUsername(username);
        for (Distribution distribution : distributions) {
            post = postRepository.findByMeetingFullNameAndTitleAndType(distribution.getMeetingFullName(), distribution.getFileTitle(), 1);
            if (post != null)
                posts.add(post);
            post = postRepository.findByMeetingFullNameAndTitleAndType(distribution.getMeetingFullName(), distribution.getFileTitle(), 2);
            if (post != null)
                posts.add(post);
        }
        Iterable<Meeting> meetings = meetingRepository.findMeetingByChair(username);
        for (Meeting meeting : meetings) {
            Iterable<Post> posts1 = postRepository.findByMeetingFullName(meeting.getFullName());
            if (posts1 == null) break;
            for (Post post1 : posts1) {
                if (!posts.contains(post1))
                    posts.add(post1);
            }
        }
        return posts;
    }

    public List<Rebuttal> allRebuttalIReceived(String username) {
        List<Rebuttal> rebuttals = new ArrayList<>();
        Rebuttal rebuttal;
        Iterable<Distribution> distributions = distributionRepository.findByUsername(username);
        for (Distribution distribution : distributions) {
            if (distribution.getStatus() == 2) {
                rebuttal = rebuttalRepository.findByMeetingFullNameAndTitle(distribution.getMeetingFullName(), distribution.getFileTitle());
                if (rebuttal != null)
                    rebuttals.add(rebuttal);
            }
        }
        Iterable<Meeting> meetings = meetingRepository.findMeetingByChair(username);
        for (Meeting meeting : meetings) {
            if (meeting.getStatus() == 4) {
                Iterable<Rebuttal> rebuttals1 = rebuttalRepository.findByMeetingFullName(meeting.getFullName());
                if (rebuttals1 == null) break;
                for (Rebuttal rebuttal1 : rebuttals1)
                    if (!rebuttals.contains(rebuttal1))
                        rebuttals.add(rebuttal1);
            }
        }
        return rebuttals;
    }

    public Post getPosts(OpenPostRequest request) {
        return postRepository.findByMeetingFullNameAndTitleAndType(request.getMeetingFullName(), request.getTitle(), request.getType());
    }
}
